package com.eblacorp.archive.view.bean.drawingdocument;

import java.io.File;
import java.io.Serializable;
import java.util.List;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.SessionScoped;

import org.apache.commons.lang.SerializationUtils;
import org.icefaces.ace.component.fileentry.FileEntry;
import org.icefaces.ace.component.fileentry.FileEntryEvent;
import org.icefaces.ace.component.fileentry.FileEntryResults;

import com.eblacorp.archive.domain.fn.DrawingDocument;
import com.eblacorp.archive.service.DrawingDocumentService;
import com.eblacorp.archive.view.utils.FacesUtils;

@ManagedBean
@SessionScoped
public class DrawingDocumentBean implements Serializable {

	private DrawingDocument drawingDocument;
	private DrawingDocument searchDocument;
	@ManagedProperty(value = "#{drawingDocumentService}")
	private DrawingDocumentService drawingDocumentService;
	private File file;
	private List<DrawingDocument> searchResult;
	private String url;
	private Integer editIndex;
	
	public DrawingDocumentBean() {
		drawingDocument = new DrawingDocument();
		searchDocument=new DrawingDocument();
	}

	public void save() {
		try {
			DrawingDocument newDocument = null;
			if (editIndex != null) {
				newDocument = drawingDocumentService.updateDocument(drawingDocument, file);
				searchResult.set(editIndex.intValue(), newDocument);
			} else {
				newDocument = drawingDocumentService.addNewDocument(drawingDocument, null);
			}
			if (newDocument != null) {
				FacesUtils.addMsg("msg.lable.saved",FacesMessage.SEVERITY_INFO, "");
			} else {
				FacesUtils.addMsg("msg.lable.faild",FacesMessage.SEVERITY_ERROR, "");
			}
		} catch (Exception e) {
			FacesUtils.addMsg("msg.lable.exception",FacesMessage.SEVERITY_ERROR, "");
			e.printStackTrace();
		} finally {
			drawingDocument = new DrawingDocument();
			editIndex=null;
			file=null;
		}
	}
	public void edit(Integer index){
		editIndex=index;
		drawingDocument=(DrawingDocument) SerializationUtils.clone(searchResult.get(index.intValue()));
		
	}

	public void fileListener(FileEntryEvent e) {
		FileEntry fe = (FileEntry) e.getComponent();
		FileEntryResults results = fe.getResults();
		File parent = null;

		// get data About File
		for (FileEntryResults.FileInfo i : results.getFiles()) {

			if (i.isSaved()) {

				file = i.getFile();

			} else {
			}
		}
	}

	public void search() {
		try {
			searchResult = drawingDocumentService.search(drawingDocument, null,
					null, false);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public void delete(Integer Index){
		
		try {
			DrawingDocument document=searchResult.get(Index.intValue());
			drawingDocumentService.deleteDocument(document, true);
			searchResult.remove(Index.intValue());
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public void getViewerUrl(Integer Index){
		url= drawingDocumentService.getViewerUrl(searchResult.get(Index.intValue()));
	}

	public DrawingDocument getDrawingDocument() {
		return drawingDocument;
	}

	public void setDrawingDocument(DrawingDocument drawingDocument) {
		this.drawingDocument = drawingDocument;
	}

	public DrawingDocumentService getDrawingDocumentService() {
		return drawingDocumentService;
	}

	public void setDrawingDocumentService(
			DrawingDocumentService drawingDocumentService) {
		this.drawingDocumentService = drawingDocumentService;
	}

	public List<DrawingDocument> getSearchResult() {
		return searchResult;
	}

	public void setSearchResult(List<DrawingDocument> searchResult) {
		this.searchResult = searchResult;
	}

	public DrawingDocument getSearchDocument() {
		return searchDocument;
	}

	public void setSearchDocument(DrawingDocument searchDocument) {
		this.searchDocument = searchDocument;
	}

	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	public Integer getEditIndex() {
		return editIndex;
	}

	public void setEditIndex(Integer editIndex) {
		this.editIndex = editIndex;
	}

	

}
